﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_MSDS_Section07
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Label
        Translate()
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            hidMSDSID.Value = IIf(IsNothing(Request.QueryString("Id")), 0, CInt(Request.QueryString("Id")))
            ShowData()
        End If
    End Sub
    Protected Sub Translate()
        'lblSection07Header.Text = getTranslate("lblSection07Header", Session("LanguageId"), lblSection07Header.Text)
        'lblS711.Text = getTranslate("lblS711", Session("LanguageId"), lblS711.Text)
        'lblS721.Text = getTranslate("lblS721", Session("LanguageId"), lblS721.Text)
        'lblS731.Text = getTranslate("lblS731", Session("LanguageId"), lblS731.Text)
        'lblS741.Text = getTranslate("lblS741", Session("LanguageId"), lblS741.Text)
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            'Tiêu đề doanh nghiệp
            Dim lang = CInt(Session("LanguageId"))
            Dim Id As Integer = hidMSDSID.Value
            Dim p As MSDSSection07 = (From a In data.MSDSSection07 Where a.MSDSId = Id And a.LanguageId = lang).SingleOrDefault
            If Not IsNothing(p) Then
                hidID.Value = p.Section07Id

                'Content
                txtS711.Text = IIf(IsNothing(p.S711) = True, "", p.S711)
                txtS721.Text = IIf(IsNothing(p.S721) = True, "", p.S721)
                txtS731.Text = IIf(IsNothing(p.S731) = True, "", p.S731)
                txtS741.Text = IIf(IsNothing(p.S741) = True, "", p.S741)
            End If
           
        End Using
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Try
                Dim p As MSDSSection07
                p = (From q In data.MSDSSection07
                Where q.Section07Id = hidID.Value
                  ).FirstOrDefault()
                Dim _isNew = True
                If Not p Is Nothing Then
                    _isNew = False
                Else
                    p = New MSDSSection07
                End If
                With p
                    .S711 = txtS711.Text.Trim
                    .S721 = txtS721.Text.Trim
                    .S731 = txtS731.Text.Trim()
                    .S741 = txtS741.Text.Trim()
                    .LanguageId = Session("LanguageId")
                    .MSDSId = hidMSDSID.Value
                End With
                If _isNew Then
                    p.Created = Now
                    p.Creator = Session("UserName")
                    data.MSDSSection07.AddObject(p)
                Else
                    p.Modified = Now
                    p.Modifier = Session("UserName")
                End If
                data.SaveChanges()

                Insert_App_Log(If(_isNew, "Insert", "Update") & "  MSDS Section07:" & hidMSDSID.Value & "", Function_Name.MSDSOriginal, If(_isNew, Audit_Type.Create, Audit_Type.Edit), Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("AlertboxRedirect('Lưu dữ liệu thành công.','../../Page/MSDS/Section08.aspx?Id=" & hidMSDSID.Value & "');", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            Finally
                GC.Collect()
            End Try
        End Using
    End Sub
    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        Response.Redirect("../../Page/MSDS/Section06.aspx?Id=" & hidMSDSID.Value)
    End Sub
#End Region
End Class
